package chapter3to7.src.main.java.com.kaikeba.concurrent;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author 吴世凯
 * 邮箱：
 * 日期：3/21/2021
 */
public class TestThreads {


    public static void main(String[] args) throws InterruptedException {


        long start = System.currentTimeMillis();

        final Random random = new Random();


        final ArrayList<Integer> list = new ArrayList<>();

        for (int i = 0; i < 100000; i++) {
            Thread thread = new Thread(() -> {
                list.add(random.nextInt());
            });
            thread.start();
            thread.join();  //让10w 个线程等下来， 并到主线程
        }

        System.out.println("time = " + (System.currentTimeMillis() - start));
        System.out.println("list.size() = " + list.size());

    }


}



/*
if  没有 thread.join 就是下面这种情况
* time = 6408
list.size() = 99970
*
* */